package controller;

import util.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *   @Description
 *   @author 高杰  gao4433@qq.com   
 *   @date 2021/12/16 15:20
 *   @since 1.8
 */
public class UserUpdateServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1. 取值
        String name = request.getParameter("n");
        String username = request.getParameter("u");
        String id = request.getParameter("i");

        // 2. 储值
        if (id != null && !"null".equals(id)) {
            // 存在 id，执行更新
            String sql = "UPDATE `user` SET `name` = ?, username = ? WHERE id = ?";
            JdbcUtils.modify(sql, name, username, id);
        } else {
            // 存在 id，执行插入
            String sql = "INSERT INTO `user` (username, `password`, `name`) VALUES(?, MD5('123'), ?)";
            JdbcUtils.modify(sql, username, name);
        }
        // 3. 跳转
        response.sendRedirect("/user-list.jsp");
    }
}

